# -*- coding:utf-8 -*-

from scrapy import Request
from scrapy.spiders import Spider
from scrapyExercise.items import JingdongItem
from scrapy_splash import SplashRequest

# splash lua script
lua_script = """
    function main(splash, args)
        splash:go(args.url)
        splash:wait(args.wait)
        splash:runjs("window.scrollTo(0, document.body.scrollHeight)")
        splash:wait(args.wait)
        return {
            html = splash:html(),
            png = splash:png(),
            log = 'this log'
            }
    end
"""

class PhoneSpider(Spider):
    name = "iphone"
    url = "https://search.jd.com/Search?keyword=iphone&enc=utf-8&wq=iphone"

    def start_requests(self):
        yield SplashRequest(self.url,
                            callback=self.parse,
                            endpoint='execute',
                            args={"lua_source": lua_script,
                                  'images': 0,
                                  'wait': 3},
                            cache_args=['lua_source'])

    def parse(self, response, **kwargs):
        item = JingdongItem()
        list_selector = response.xpath("//div[@id='J_goodsList']/ul/li")
        for one_selector in list_selector:
            print('-')